/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 UJF.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.mock;

import static org.osami.gateway.sensor.SenseNode.TOPIC_PREFIX;

import java.util.HashMap;
import java.util.Map;

import org.osami.gateway.sensor.SenseData;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.util.measurement.Measurement;

/**
 * The SenseDataProducer component produce a lot of Mock SenseData.
 */
public class SenseDataProducer {

    private String senseId = "mockTemperature";

    private static final String SENSE_TYPE = "temperature";

    private static final String SENSE_UNIT = "celsius";

    private long min;

    private EventAdmin eventAdmin;

    private long period;

    private volatile boolean running = false;

    private void start() {
        Runnable producer = new Producer();
        running = true;
        new Thread(producer).start();
    }

    private void stop() {
        running = false;
    }

    private class Producer implements Runnable {

        Map<String, Object> properties;

        int count = 0;

        public void run() {
            try {
                while (running) {
                    properties = new HashMap<String, Object>();
                    properties.put(SenseData.DEFAULT_DATA_KEY, new SenseDataImpl());
                    eventAdmin.postEvent(new Event(TOPIC_PREFIX + senseId, properties));

                    Thread.sleep(period);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private class SenseDataImpl implements SenseData {

        private long timestamp;

        private double value;

        public SenseDataImpl() {
            timestamp = System.currentTimeMillis();
            value = (Math.random() * 30) + min;
        }

        public String getSenseId() {
            return senseId;
        }

        public Long getTimeStamp() {
            return timestamp;
        }

        public String getType() {
            return SENSE_TYPE;
        }

        public String getUnit() {
            return SENSE_UNIT;
        }

        public Double getValue() {
            return value;
        }

        public Measurement toMeasurement() {
            return new Measurement(value);
        }
    }
}
